/*
 * user_examine.c
 *
 *  Created on: 2011-10-29
 *      Author: poseidon
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

#include "types.h"
#include "define.h"
#include "user_examine.h"
#include "net.h"

unsigned int user_register(struct user * user_struct) {
	FILE *file;
	ssize_t writen;
	file = fopen(userconf, "a");
	if (file == NULL) {
		perror("fopen");
		return -1;
	}
	writen = fwrite(user_struct, sizeof(user_struct), 1, file);
	if (writen != sizeof(user_struct)) {
		perror("fwrite");
		return -1;
	}
	fclose(file);
	return 0;
}

unsigned int user_check(struct user * user_struct) {
	FILE *file;
	ssize_t readen;
	struct user users[MAX_CONNECTED];
	int user_num = 0;
	file = fopen(userconf, "r");
	if (file == NULL) {
		perror("fopen");
		return -1;
	}
	while(!feof(file) && user_num < MAX_CONNECTED) {
		if (!feof(file)) {
			fread(users[user_num], sizeof(struct user), 1, file);
		} else {
			break;
		}
		user_num += 1;
	}
	fclose(file);
	int i = 0;
	for (i = 0; i < user_num; i ++) {
		if ((strcmp(user_struct.username, users[i].username) == 0) && (strcmp(user_struct.password, users[i].password) == 0)) {
			printf("User: %s auth passed\n", user_struct.username);
			return 0;
		}
	}
	return -1;
}




















